package com.link.base.base.redpacketconfig.service;

import javax.annotation.Resource;

import com.link.base.base.member.util.MpPayConstants;
import com.link.base.base.redpacketconfig.model.RedPktPayCfg;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;
import com.link.base.base.redpacketconfig.dao.mybatis.mapper.RedPktPayCfgMapper;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


/**
 * @author: 闫有霞
 * @date: 2019-01-23
 * @comments: 红包付款设置
 */
@Service
public class RedPktPayCfgServiceImpl extends BasicServiceImpl<RedPktPayCfg> implements RedPktPayCfgService {

    @Resource
    private RedPktPayCfgMapper redPktPayCfgMapper;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<RedPktPayCfg> getBasicMapper() {
        return redPktPayCfgMapper;
    }

    /**
     *@createdBy 闫有霞
     *@date 2019/1/23
     *@methods: loadPayCfgToRedis
     *@param:   RedPktPayCfg entity
     *@return:  void
     *@description: 加载付款设置数据到Redis
     **/
    @Override
    public void loadPayCfgToRedis(RedPktPayCfg entity) throws Exception {
        LinkRedisClient jedis = null;
        try {
            // 付款设置
            jedis = RedisUtil.getJedis();
            // 单日付款最大预警金额
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.DAY_ALERT_MAX_PAY,String.valueOf(entity.getDayPayAlertAmount()));
            // 单日最大预警通知电话号码
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.DAY_ALERT_MAX_PHONE,String.valueOf(entity.getDayPayAlertPhone()));
            // 单日最大预警提示模板
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.DAY_ALERT_MAX_MSG_TEMPLATE,String.valueOf(entity.getDayPayAlertMsg()));

            // 单人单日最大付款金额，到达后停止付款
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.DAY_LIMIT_MAX_PAY_AMOUNT,String.valueOf(entity.getDayPayMaxAmount()));

            // 当人单日最大付款次数，到达后停止付款
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY, MpPayConstants.DAY_LIMIT_MAX_PAY_NUM, String.valueOf(entity.getDayPayMaxNum()));

            // 付款超频预警时间范围
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_LIMIT_TIME,String.valueOf(entity.getPayLimitTime()));
            // 付款超频预警数量
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_LIMIT_NUM,String.valueOf(entity.getPayLimitNum()));
            // 付款超频预警通知号码
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_LIMIT_PHONES,String.valueOf(entity.getPayLimitPhones()));
            // 付款超频预警通知短信模板


            //付款超频预警时间范围
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_OPENID_LIMIT_TIME,String.valueOf(entity.getOpenidReqLimitTime()));
            //付款超频预警数量
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_OPENID_LIMIT_NUM,String.valueOf(entity.getOpenidReqLimitNum()));
            //付款黑名单解锁时长
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_OPENID_REQUEST_TIME_INTERVAL,
                    String.valueOf(entity.getOpenidReqLimitLock()));
            //付款异常通知信息
            jedis.hset(MpPayConstants.MP_PAY_CONFIG_KEY,MpPayConstants.MP_PAY_OPENID_ALERT_MSG,entity.getOpenidReqErrMsg());

        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }


    }

    /**
     * @createdBy 闫有霞
     * @date 2019/1/23
     * @methods: insertPayCfg
     * @param: RedPktPayCfg entity
     * @return: void
     * @description: 添加红包付款设置数据
     **/

    @Override
    public void insertPayCfg(RedPktPayCfg entity) throws Exception {
        if (entity != null) {
            Long id = keyGenerateService.keyGenerate();
            Long corpid = UserUtil.getUser().getLoginCorpId();
            entity.setId(id);
            entity.setCorpid(corpid);
            redPktPayCfgMapper.insert(entity);
            loadPayCfgToRedis(entity);
        }
    }


    /**
     *@createdBy 闫有霞
     *@date 2019/1/23
     *@methods: updatePayCfg
     *@param: RedPktPayCfg entity
     *@return: void
     *@description: 更新红包付款设置数据
     **/

    @Override
    public void updatePayCfg(RedPktPayCfg entity) throws Exception {
        if (entity != null) {
            redPktPayCfgMapper.update(entity);
            loadPayCfgToRedis(entity);
        }
    }
}

